package stu.aistar.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundListOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import stu.aistar.dao.IMovieDao;
import stu.aistar.model.pojo.*;
import stu.aistar.model.vo.DataJsonVo;
import stu.aistar.service.IAreaService;
import stu.aistar.service.ICinameService;
import stu.aistar.service.IMovieService;
import stu.aistar.service.MovieTypeService;
import stu.aistar.util.FileUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/index")
public class Movie1Controller {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IAreaService areaService;

    @Autowired
    private IMovieDao movieDao;

    @Autowired
    private ICinameService cinameService;

    @Autowired
    private IMovieService movieService;



    @GetMapping("/movie")
    public String index(Model model,String name, Integer cid,Integer aid,Integer cinameId){
        //ListOperations<String,Movie> listOperations = redisTemplate.opsForList();

        //获取地区
        List<Area> citys;
        ListOperations<String,Area> lop2 = redisTemplate.opsForList();
        BoundListOperations ops = redisTemplate.boundListOps("citys");
        if(null == ops || ops.size()==0){
            System.out.println("从数据库取======================================");
            citys = areaService.findCitys();
            for (Area city : citys) {
                lop2.leftPush("citys",city);
            }
        }else{    //从缓存中取
            System.out.println("从缓存取======================================");
            citys = lop2.range("citys",0,-1);
        }
        model.addAttribute("citys",citys);
        //前端传过来-1

        String cid2=null,aid2=null;
        if(null!=cid&&-1!=cid){
            ListOperations<String,Area> lop3 = redisTemplate.opsForList();
            BoundListOperations ops1 = redisTemplate.boundListOps("areas:" + cid);
            List<Area> areas;
            if(null == ops1 || ops1.size()==0){
                areas  = areaService.findAreas(cid);
                for (Area a: areas) {
                    lop3.leftPush("areas:"+cid,a);
                }
            }else {
                areas = lop3.range("areas:"+cid,0,-1);
            }
            model.addAttribute("areas",areas);
            model.addAttribute("cid",cid);
            cid2 = cid.toString();
        }
        if(null!=aid&&-1!=aid){
            model.addAttribute("aid",aid);
            aid2=aid.toString();
        }

        List<Ciname> cinames = cinameService.findCinamesByArea(cid2,aid2);
        model.addAttribute("cinames",cinames);
        model.addAttribute("cinameId",cinameId);
        String ciname_id = null;
        if(null!=cinameId&&-1!=cinameId){
            ciname_id = cinameId.toString();
        }

        List<Movie> movies = movieService.findByConditionals(name, cid2, aid2, ciname_id);
        model.addAttribute("list",movies);
        model.addAttribute("name",name);

        return "index";
    }

    @RequestMapping("/getCity")
    @ResponseBody
    public void getCity(HttpServletRequest request,Integer cid,Integer aid){
        String cid2=null,aid2=null;
        if(null!=cid&&-1!=cid){
            ListOperations<String,Area> lop3= redisTemplate.opsForList();
            BoundListOperations ops1 = redisTemplate.boundListOps("areas:" + cid);
            List<Area> areas;
            if(null == ops1 || ops1.size()==0){
                areas  = areaService.findAreas(cid);
                for (Area a: areas) {
                    lop3.leftPush("areas:"+cid,a);
                }
            }else {
                areas = lop3.range("areas:"+cid,0,-1);
            }
            request.setAttribute("areas",areas);
            request.setAttribute("cid",cid);
            cid2 = cid.toString();
        }
        if(null!=aid&&-1!=aid){
            request.setAttribute("aid",aid);
            aid2=aid.toString();
        }
    }

}
